import request from '@/utils/request'

export function listCategories() {
  return request({
    url: '/SysFeedingRecords/SysInventory/categories',
    method: 'get'
  }).then(response => {
    return response.data; // 假设返回格式：["category1", "category2"]
  });
}

export function listNamesByCategory(category) {
  return request({
    url: '/SysFeedingRecords/SysInventory/materials',
    method: 'get',
    params: { category }
  }).then(response => {
    // 确保返回物资名称数组
    return response.data || [];
  });
}

export function fetchBatchRecords(category, materialName) {
  return request({
    url: '/SysFeedingRecords/SysInventory/batches',
    method: 'get',
    params: { category, materialName }
  }).then(response => {
    console.log('批次接口原始响应:', response);
    
    // 提取批次号数组（适配不同响应格式）
    let batchData = [];
    if (Array.isArray(response.data)) {
      batchData = response.data;
    } else if (Array.isArray(response.data?.data)) {
      batchData = response.data.data;
    }
    
    // 仅返回批次号和id（编号后续通过listBatches获取）
    return batchData.map(batchNum => ({
      id: batchNum,
      batchNumber: batchNum
    }));
  }).catch(err => {
    console.error('批次接口请求失败:', err);
    return [];
  });
}
export function listBatches(category, materialName, batchNumber) {
  return request({
    url: "/SysFeedingRecords/SysInventory/record-ids",
    method: "GET",
    params: { category, materialName, batchNumber }
  });
}
